//
//  GXBaseViewController.m
//  GxDemo
//
//  Created by 甘霞 on 15/10/23.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXBaseViewController.h"
#import "FontManager.h"                  // 字体管理器
#import "UIButton+GXCircleImageButton.h" // wait
#import "MBProgressHUD.h"                // 菊花

#define kButtomViewHeight 35
#define kButtomBtnSize 35
#define kButtomViewSideBorder 15

@interface GXBaseViewController ()

@end

@implementation GXBaseViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    //设置 tableView 颜色
//    self.tableView.backgroundColor = isNight ? kIsNightColor_TableView : kIsDayColor_TableView;
//    self.tableView.alpha = 1;
//    //设置背景颜色
//    self.collectionView.backgroundColor = isNight ? kIsNightColor_CollectionView : kIsDayColor_CollectionView;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeModel:) name:@"changeModel" object:nil];
}

- (void)changeModel:(NSNotification *)note {
    [self.tableView reloadData]; // ??
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navi];
    self.manager = [CoreDataManager shareInstance];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)navi {
    //将导航栏设置为透明色
    [self.navigationController.navigationBar setTranslucent:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //将导航栏设置为全透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"talkingbubble-gray"] forBarMetrics:UIBarMetricsCompact];
    // UIBarMetricsCompact ~>
    // Specifies metrics when using the phone idiom.
    
}

#pragma mark - 创建 tableView
- (void)createTableViewWithFrame:(CGRect)frame {
    self.tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.view addSubview:self.tableView];
    // 注册 
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"cell"];
    self.tableView.tableFooterView = [UIView new]; // ??
}

#pragma mark - 创建 collectionView
- (void)createCollectionViewWithFrame:(CGRect)frame {
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.layout.minimumInteritemSpacing = 0;
    self.layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:self.layout];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView"];
}

#pragma mark - 创建 WebView
- (void)createWebViewWithFrame:(CGRect)frame URL:(NSURL *)url{
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenHeight)]; //??
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
    [self.webView reload];
}

//- (void)createWKWebViewWithFrame:(CGRect)frame URL:(NSURL *)url{
//    self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenHeight)];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    [self.wkWebView loadRequest:request];
//    [self.view addSubview:self.wkWebView];
//}

#pragma mark - 创建底部按钮视图
- (void)createButtomView {
    self.buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kButtomBtnSize - kButtomViewSideBorder, kScreenWidth, kButtomViewHeight)];
    [self.view addSubview:self.buttonView];
    
}

#pragma mark - 进入详情页时底栏按钮
//分享 后退
- (void)createButtomBackButtonIsHiddenShareBtn:(Boolean)isHidden{
    
    //创建底部按钮视图
    [self createButtomView];
    
    //创建后退按钮
    self.backBtn = [UIButton initBtnWithFrame:CGRectMake(kButtomViewSideBorder, 0, kButtomBtnSize, kButtomBtnSize) tag:1000 image:@"bottom_btn_back_active"];
    self.backBtn.alpha = 0.8;
    [self.buttonView addSubview:self.backBtn];
    
    //创建分享按钮
    if (!isHidden) {
        self.shareBtn = [UIButton initBtnWithFrame:CGRectMake(kScreenWidth - kButtomBtnSize - kButtomViewSideBorder, 0, kButtomBtnSize, kButtomBtnSize) tag:2000 image:@"btn_topic_share_active"];
        self.shareBtn.alpha = 0.8;
        [self.buttonView addSubview:self.shareBtn];
    }
}

//后退 喜欢 分享 评论
- (void)createButtomLikeButton {
    //创建后退视图
    [self createButtomBackButtonIsHiddenShareBtn:YES];
    // 分享按钮
    self.repostBtn = [UIButton initBtnWithFrame:CGRectMake(kScreenWidth - kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:4000 image:@"bottom_btn_share_active"];
    [self.buttonView addSubview:self.repostBtn];
    
    //创建喜欢按钮
    CGFloat repostBtnX = CGRectGetMinX(self.repostBtn.frame);
    self.likeBtn = [UIButton initBtnWithFrame:CGRectMake(repostBtnX - 2 * kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:5000 image:@"bottom_btn_like_active"];

    [self.buttonView addSubview:self.likeBtn];
    self.flashImage = [UIImageView initImageViewWithFrame:CGRectMake(repostBtnX - 2 * kButtomViewSideBorder - kButtomBtnSize + 2, -5, kButtomBtnSize, kButtomBtnSize) tag:5000 image:@""];
    [self.buttonView addSubview:self.flashImage];
}

- (void)createButtomUnLikeButton {
    //创建后退视图
    [self createButtomBackButtonIsHiddenShareBtn:YES];
    // 分享按钮
    self.repostBtn = [UIButton initBtnWithFrame:CGRectMake(kScreenWidth - kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:4000 image:@"bottom_btn_share_active"];
    [self.buttonView addSubview:self.repostBtn];
    
    //创建喜欢按钮
    CGFloat repostBtnX = CGRectGetMinX(self.repostBtn.frame);
    self.likeBtn = [UIButton initBtnWithFrame:CGRectMake(repostBtnX - 2 * kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:5000 image:@"bottom_btn_liked"];
    [self.buttonView addSubview:self.likeBtn];
    self.flashImage = [UIImageView initImageViewWithFrame:CGRectMake(repostBtnX - 2 * kButtomViewSideBorder - kButtomBtnSize + 2, -5, kButtomBtnSize, kButtomBtnSize) tag:5000 image:@""];
    [self.buttonView addSubview:self.flashImage];
}

- (void)createImageViewLike
{
    //创建后退视图
    [self createButtomBackButtonIsHiddenShareBtn:YES];
    // 分享按钮
    self.repostBtn = [UIButton initBtnWithFrame:CGRectMake(kScreenWidth - kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:4000 image:@"bottom_btn_share_active"];
    [self.buttonView addSubview:self.repostBtn];
    
    //创建喜欢按钮
    CGFloat repostBtnX = CGRectGetMinX(self.repostBtn.frame);
    self.flashImage = [UIImageView initImageViewWithFrame:CGRectMake(repostBtnX - 2 * kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:5000 image:@"bottom_btn_like_active"];
    [self.buttonView addSubview:self.flashImage];
}

- (void)createImageViewUnlike
{
    //创建后退视图
    [self createButtomBackButtonIsHiddenShareBtn:YES];
    // 分享按钮
    self.repostBtn = [UIButton initBtnWithFrame:CGRectMake(kScreenWidth - kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:4000 image:@"bottom_btn_share_active"];
    [self.buttonView addSubview:self.repostBtn];
    
    //创建喜欢按钮
    CGFloat repostBtnX = CGRectGetMinX(self.repostBtn.frame);
    self.flashImage = [UIImageView initImageViewWithFrame:CGRectMake(repostBtnX - 2 * kButtomViewSideBorder - kButtomBtnSize, 0, kButtomBtnSize, kButtomBtnSize) tag:5000 image:@"bottom_btn_liked"];
    [self.buttonView addSubview:self.flashImage];
}

#pragma mark - 是否隐藏底部按钮
- (void)setHiddenLikeBtnView:(Boolean)isHidden {
    for (UIButton *btn in [self.buttonView subviews]) {
        CATransition *transition = [CATransition animation];
        //  设置动画效果
        //  通过 type 的类型来控制
        transition.type = @"reveal";
        //  时间
        transition.duration = 2;
        //  重复次数
        transition.repeatCount = 1;
        //  动画方向  The transition begins at the top of the layer.
        transition.subtype = kCATransitionFromTop;
        //  添加到 layer
        [btn.layer addAnimation:transition forKey:@"dff"];
        //隐藏
        [btn setHidden:isHidden];
    }
}

#pragma mark - 提示未开放功能
- (void)uncompletedFuntion {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"该功能尚未开放" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:nil];
    [alertController addAction:sureAction]; // Attaches an action object to the alert or action sheet.
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - 小菊花
- (void)loadNightMBProgress
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    self.hud.dimBackground = NO;
    //设置对话框文字
    self.hud.labelText = @"loading";
    self.hud.minShowTime = 50;
    self.hud.activityIndicatorColor = [UIColor whiteColor];
    self.hud.labelColor = [UIColor whiteColor];
    
    [self.view addSubview:self.hud];
    [self.hud showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(1);
    } completionBlock:^{
        
        [self.hud removeFromSuperview];
        self.hud = nil;
    }];
}

- (void)removeHud
{
    [self.hud removeFromSuperview];
}


@end
